package com.suning.sawp.web.controller.member;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.exception.MobileException;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.member.constant.MemberConstants;
import com.suning.sawp.member.service.MemCustomerService;
import com.suning.sawp.member.vo.MemCustomerDetailResponse;
import com.suning.sawp.member.vo.MemCustomerResponse;
import com.suning.sawp.member.vo.MemFollowLogResponse;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 我的客户相关接口 <br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/client/member")
public class MemCustomerController extends DjBaseController {

	/**
	 * 日志
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(MemCustomerController.class);

	/**
	 * 客户服务层
	 */
	@Autowired
	MemCustomerService memCustomerService;

	/**
	 * 员工信息服务
	 */
	@Autowired
	StoreManService storeManService;

	/**
	 * 查询我的客户列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param isToday
	 * @param buyDateType
	 * @param custName
	 * @param currIndex
	 * @param pageSize
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/queryMyCustomers.do")
	public MyGsonView queryMyCustomers(Principal principal, String isToday, String buyDateType, String custName, Integer currIndex, Integer pageSize) {
		ReturnMsg<MemCustomerResponse> retMsg;
		StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
		if (null != man) {
			// 判断是否选择待跟进按钮
			boolean today = false;
			if ("1".equals(isToday)) {
				today = true;
			}
			try {
				// 查询客户列表
				retMsg = memCustomerService.queryMyCustomers(man.getStoreCode(), man.getStaffId(), custName, buyDateType, today, DJStringUtils.initCurrIndex(currIndex),
						DJStringUtils.initPageSize(pageSize));
			} catch (Exception e) {
				LOGGER.error("queryMyCustomers error,staffId:" + principal.getName(), e);
				// 数据库异常
				retMsg = new ReturnMsg<MemCustomerResponse>();
				retMsg.setError(ErrorCodeConstants.ERR_0028);
			}
		} else {
			// 店员信息异常
			retMsg = new ReturnMsg<MemCustomerResponse>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
		}
		return setGsonView(retMsg);
	}

	/**
	 * 查询我的客户详情信息 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param custId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/queryMyCustomerDetail.do")
	public MyGsonView queryMyCustomerDetail(Principal principal, Long custId) {
		ReturnMsg<MemCustomerDetailResponse> retMsg;
		// 判断客户ID是否为空
		if (null == custId || custId.longValue() < 0) {
			retMsg = new ReturnMsg<MemCustomerDetailResponse>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "custId");
		} else {
			// 查询员工信息
			StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
			if (null != man) {
				try {
					// 查询客户列表
					retMsg = memCustomerService.queryMyCustomerDetail(custId);
				} catch (Exception e) {
					LOGGER.error("queryMyCustomerDetail error,staffId:" + principal.getName(), e);
					// 数据库异常
					retMsg = new ReturnMsg<MemCustomerDetailResponse>();
					retMsg.setError(ErrorCodeConstants.ERR_0028);
				}
			} else {
				// 店员信息异常
				retMsg = new ReturnMsg<MemCustomerDetailResponse>();
				retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
			}
		}
		return setGsonView(retMsg);
	}

	/**
	 * 保存客户跟进记录 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param custId
	 * @param type
	 * @param nextTime
	 * @param result
	 * @param memo
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/saveCustomerFollow.do")
	public MyGsonView saveCustomerFollow(Principal principal, Long custId, String type, String nextTime, String result, String memo, String devNo, HttpServletRequest request) {
		ReturnMsg<String> retMsg;
		// 判断客户ID是否为空
		if (null == custId || custId.longValue() < 0) {
			retMsg = new ReturnMsg<String>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "custId");
		} else if (StringUtils.isBlank(type)) {
			retMsg = new ReturnMsg<String>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "type");
		} else if (StringUtils.isBlank(devNo)) {
			retMsg = new ReturnMsg<String>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "devNo");
		} else if (!MemberConstants.FOLLOW_TYPE_BY_PHONE.equals(type) && !MemberConstants.FOLLOW_TYPE_BY_WECHAT.equals(type)) {
			retMsg = new ReturnMsg<String>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0021, (Object) "type");
		} else {
			// 查询员工信息
			StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
			if (null != man) {
				try {
					// 查询客户列表
					retMsg = memCustomerService.saveCustomerFollow(man.getStoreCode(), man.getStaffId(), custId, Integer.parseInt(type), nextTime, memo, result, devNo,
							DJStringUtils.getUserIp(request));
				} catch (Exception e) {
					LOGGER.error("saveCustomerFollow error,staffId:" + principal.getName(), e);
					// 数据库异常
					retMsg = new ReturnMsg<String>();
					retMsg.setError(ErrorCodeConstants.ERR_0028);
				}
			} else {
				// 店员信息异常
				retMsg = new ReturnMsg<String>();
				retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
			}
		}
		return setGsonView(retMsg);
	}

	/**
	 * 查询客户跟进记录列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param custId
	 * @param currIndex
	 * @param pageSize
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/queryCustomerFollowList.do")
	public MyGsonView queryCustomerFollowList(Principal principal, Long custId, Integer currIndex, Integer pageSize) {
		ReturnMsg<MemFollowLogResponse> retMsg;
		// 判断客户ID是否为空
		if (null == custId || custId.longValue() < 0) {
			retMsg = new ReturnMsg<MemFollowLogResponse>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "custId");
		} else {
			// 查询员工信息
			StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
			if (null != man) {
				try {
					// 查询客户列表
					retMsg = memCustomerService.queryCustomerFollowList(man.getStoreCode(), man.getStaffId(), custId, DJStringUtils.initCurrIndex(currIndex), DJStringUtils.initPageSize(pageSize));
				} catch (Exception e) {
					LOGGER.error("queryCustomerFollowList error,staffId:" + principal.getName(), e);
					// 数据库异常
					retMsg = new ReturnMsg<MemFollowLogResponse>();
					retMsg.setError(ErrorCodeConstants.ERR_0028);
				}
			} else {
				// 店员信息异常
				retMsg = new ReturnMsg<MemFollowLogResponse>();
				retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
			}
		}
		return setGsonView(retMsg);
	}

	/**
	 * 查询共享客户列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param currIndex
	 * @param pageSize
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/queryShareCustomerList.do")
	public MyGsonView queryShareCustomerList(Principal principal, String employeeName, Integer currIndex, Integer pageSize) {
		ReturnMsg<MemCustomerResponse> retMsg;
		// 查询员工信息
		StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
		if (null != man) {
			try {
				// 查询客户列表
				retMsg = memCustomerService.queryShareCustomerList(man.getStoreCode(), employeeName, DJStringUtils.initCurrIndex(currIndex), DJStringUtils.initPageSize(pageSize));
			} catch (Exception e) {
				LOGGER.error("queryShareCustomerList error,staffId:" + principal.getName(), e);
				// 数据库异常
				retMsg = new ReturnMsg<MemCustomerResponse>();
				retMsg.setError(ErrorCodeConstants.ERR_0028);
			}
		} else {
			// 店员信息异常
			retMsg = new ReturnMsg<MemCustomerResponse>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
		}
		return setGsonView(retMsg);
	}

	/**
	 * 分配共享客户 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param custId
	 * @param staffId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/updateShareCustomer.do")
	public MyGsonView updateShareCustomer(Principal principal, Long custId, String staffId) {
		ReturnMsg<String> retMsg;
		// 判断客户ID是否为空
		if (null == custId || custId.longValue() < 0) {
			retMsg = new ReturnMsg<String>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "custId");
		} else if (StringUtils.isBlank(staffId)) {
			retMsg = new ReturnMsg<String>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "staffId");
		} else {
			// 查询员工信息
			StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
			if (null != man) {
				try {
					// 更新共享客户信息
					retMsg = memCustomerService.updateShareCustomer(man.getStoreCode(), custId, staffId);
				} catch (MobileException e) {
					LOGGER.error("updateShareCustomer mobileException,staffId:" + principal.getName(), e);
					retMsg = new ReturnMsg<String>();
					retMsg.setError(ErrorCodeConstants.ERR_MEM_0015, e.getMessage());
				} catch (Exception e) {
					LOGGER.error("updateShareCustomer error,staffId:" + principal.getName(), e);
					// 数据库异常
					retMsg = new ReturnMsg<String>();
					retMsg.setError(ErrorCodeConstants.ERR_MEM_0015);
				}
			} else {
				// 店员信息异常
				retMsg = new ReturnMsg<String>();
				retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
			}
		}
		return setGsonView(retMsg);
	}

	/**
	 * 查询我的潜客列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param currIndex
	 * @param pageSize
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/queryPotentialCustomerList.do")
	public MyGsonView queryPotentialCustomerList(Principal principal, Integer currIndex, Integer pageSize) {
		ReturnMsg<MemCustomerResponse> retMsg;
		// 查询员工信息
		StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
		if (null != man) {
			try {
				// 查询潜客列表
				retMsg = memCustomerService.queryPotentialCustomerList(man.getStoreCode(), man.getStaffId(), DJStringUtils.initCurrIndex(currIndex), DJStringUtils.initPageSize(pageSize));
			} catch (Exception e) {
				LOGGER.error("queryPotentialCustomerList error,staffId:" + principal.getName(), e);
				// 数据库异常
				retMsg = new ReturnMsg<MemCustomerResponse>();
				retMsg.setError(ErrorCodeConstants.ERR_0028);
			}
		} else {
			// 店员信息异常
			retMsg = new ReturnMsg<MemCustomerResponse>();
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
		}
		return setGsonView(retMsg);
	}

	/**
	 * 更改员工客户为共享客户 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param principal
	 * @param staffId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/updateToShareCustomer.do")
	public MyGsonView updateToShareCustomer(Principal principal, String staffId) {
		ReturnMsg<String> retMsg = new ReturnMsg<String>();
		// 判断客户ID是否为空
		if (StringUtils.isBlank(staffId)) {
			retMsg.setError(ErrorCodeConstants.ERR_MEM_0020, (Object) "staffId");
		} else {
			// 查询员工信息
			StoreManDto man = storeManService.queryStoreManAttachInfo(principal.getName());
			if (null != man) {
				try {
					// 更新共享客户信息
					memCustomerService.updateToShareCustomer(staffId);
				} catch (Exception e) {
					LOGGER.error("updateToShareCustomer error,staffId:" + principal.getName(), e);
					// 数据库异常
					retMsg.setError(ErrorCodeConstants.ERR_0028);
				}
			} else {
				// 店员信息异常
				retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
			}
		}
		return setGsonView(retMsg);
	}

	@RequestMapping("/index.do")
	public MyGsonView index(Principal principal) {
		String staffId = principal.getName();
		return setGsonViewWithEmpty(memCustomerService.index(staffId));
	}

}
